package com.yiyahui.project.erp.controller;

import java.util.List;

import com.yiyahui.project.erp.domain.requestVo.YiyaPlanListRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import com.yiyahui.framework.aspectj.lang.annotation.Log;
import com.yiyahui.framework.aspectj.lang.enums.BusinessType;
import com.yiyahui.project.erp.domain.YiyaPlan;
import com.yiyahui.project.erp.service.IYiyaPlanService;
import com.yiyahui.framework.web.controller.BaseController;
import com.yiyahui.framework.web.domain.AjaxResult;
import com.yiyahui.common.utils.poi.ExcelUtil;
import com.yiyahui.framework.web.page.TableDataInfo;

/**
 * 排班Controller
 * 
 * @author yiyahui
 * @date 2020-05-16
 */
@Api("排班")
@RestController
@RequestMapping("/erp/plan")
public class YiyaPlanController extends BaseController
{
    @Autowired
    private IYiyaPlanService yiyaPlanService;
 
    /**
     * 查询排班列表
     */
    @ApiOperation("查询排班列表")
    @PreAuthorize("@ss.hasPermi('erp:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(YiyaPlan yiyaPlan)
    {
        startPage();
        List<YiyaPlan> list = yiyaPlanService.selectYiyaPlanList(yiyaPlan);
        return getDataTable(list);
    }

    /**
     * 导出排班列表
     */
    @ApiOperation("导出排班列表")
    @PreAuthorize("@ss.hasPermi('erp:plan:export')")
    @Log(title = "排班", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(YiyaPlan yiyaPlan)
    {
        List<YiyaPlan> list = yiyaPlanService.selectYiyaPlanList(yiyaPlan);
        ExcelUtil<YiyaPlan> util = new ExcelUtil<YiyaPlan>(YiyaPlan.class);
        return util.exportExcel(list, "plan");
    }

    /**
     * 获取排班详细信息
     */
    @ApiOperation("获取排班详细信息")
    @ApiImplicitParam(name = "id", value = "主键id", required = true, dataType = "Long", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:plan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yiyaPlanService.selectYiyaPlanById(id));
    }

    /**
     * 新增排班
     */
    @ApiOperation("新增排班")
    @ApiImplicitParam(name = "yiyaPlan", value = "${tableComment}", dataType = "YiyaPlan")
    @PreAuthorize("@ss.hasPermi('erp:plan:add')")
    @Log(title = "排班", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YiyaPlan yiyaPlan)
    {
        return toAjax(yiyaPlanService.insertYiyaPlan(yiyaPlan));
    }

    /**
     * 修改排班
     */
    @ApiOperation("修改排班")
    @ApiImplicitParam(name = "yiyaPlan", value = "${tableComment}", dataType = "YiyaPlan")
    @PreAuthorize("@ss.hasPermi('erp:plan:edit')")
    @Log(title = "排班", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YiyaPlan yiyaPlan)
    {
        return toAjax(yiyaPlanService.updateYiyaPlan(yiyaPlan));
    }

    /**
     * 删除排班
     */
    @ApiOperation("删除排班")
    @ApiImplicitParam(name = "ids", value = "主键id数组", required = true, dataType = "Long[]", paramType = "path")
    @PreAuthorize("@ss.hasPermi('erp:plan:remove')")
    @Log(title = "排班", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yiyaPlanService.deleteYiyaPlanByIds(ids));
    }


    /**
     * 批量新增排班
     */
    @ApiOperation("批量新增排班")
    @ApiImplicitParam(name = "批量新增排班")
//    @PreAuthorize("@ss.hasPermi('erp:plan:batchadd')")
    @Log(title = "排班", businessType = BusinessType.INSERT)
    @PostMapping("/batchAdd")
    public AjaxResult batchAdd(@RequestBody YiyaPlanListRequest yiyaPlans)
    {
        if (yiyaPlans.getYiyaPlans() != null && yiyaPlans.getYiyaPlans().size() > 0) {
            yiyaPlans.getYiyaPlans().forEach(
                    r -> yiyaPlanService.insertYiyaPlan(r)
            );
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }
}
